package com.samshow.config;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;

import java.util.Collection;

/**
 * Created by Administrator on 2017/1/13.
 */

public class GateWayAccessDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> collection) throws AccessDeniedException, InsufficientAuthenticationException {
        //SampleUserDetail user = (SampleUserDetail) authentication.getAuthorities();
       long authCount =  collection.stream().filter(configAttribute->{
            long count = authentication.getAuthorities().stream().filter(grantedAuthority->{
                if (grantedAuthority.getAuthority().equals(configAttribute.getAttribute())){
                    return true;
                }else {
                    return false;
                }
            }).count();
            if (count>0)
                return true;
            else
                return false;
        }).count();
       if (authCount==0){
           throw new AccessDeniedException("权限不足!");
       }
    }

    @Override
    public boolean supports(ConfigAttribute configAttribute) {
        return true;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
